package com.sgb.kjwl.viewmodel.self.driver;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.self.AddDriverAdress;
import com.sgb.kjwl.model.entity.self.LicenceOCREntity;
import com.sgb.kjwl.model.entity.self.MotorcadeSimpleEntity;
import com.sgb.kjwl.model.entity.self.ProDriverRegisterEntity;
import com.sgb.kjwl.model.response.ListResponse2;
import com.sgb.kjwl.utils.AddressHelper;
import com.swgk.core.util.UserOperating;
import com.sgb.kjwl.view.ui.activity.self.driver.NewProprietaryDriverActivity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Describe:
 * Author:  yangLei
 * Date:    2021/10/12
 */
public class NewProDriverModel extends BaseViewModel {

    public MutableLiveData<List<MotorcadeSimpleEntity>> motorcadeListLD = new MutableLiveData<>();
    public MutableLiveData<String> registerLD = new MutableLiveData<>();
    public MutableLiveData<Boolean> codeLD = new MutableLiveData<>();
    public MutableLiveData<LicenceOCREntity> OCRLD = new MutableLiveData<>();
    private AddDriverAdress ldDriverAdress = new AddDriverAdress();

    public NewProDriverModel(@NonNull @NotNull Application application) {
        super(application);
    }

    /**
     * 请求所有的自营车队列表
     */
    public void requestMotorcadeList() {
        Map<String, Object> map = new HashMap<>();
        map.put("type", 1);//合作类型 1 自营 2 挂靠
        map.put("page", 1);
        map.put("limit", 99);
        NewAPIManager.getInstance().getLogisticsAPI().getMotorcadeList(map).enqueue(new ICallBack<BaseEntity<ListResponse2<List<MotorcadeSimpleEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse2<List<MotorcadeSimpleEntity>>> data) {
                if (data != null) {
                    if (data.getData() != null) {
                        motorcadeListLD.postValue(data.getData().getList());
                    } else {
                        motorcadeListLD.postValue(null);
                    }
                }
            }
        });
    }

    /**
     * 上传资料并注册
     */
    public void uploadDataRegisterDriver(NewProprietaryDriverActivity activity, ProDriverRegisterEntity entity) {
        DialogHelper.showProgressDialog(activity, "请求中...");
        Map<String, Object> map = new HashMap<>();
        map.put("realName", entity.getRealName());
        map.put("identityCard", entity.getIdentityCard());
        if (UserOperating.getInstance().isCompany()) {
            map.put("motorcadeName", entity.getMotorcadeName());
            map.put("motorcadeNo", entity.getMotorcadeNo());
        }
        map.put("telPhone", entity.getTelPhone());
        map.put("code", entity.getCode());
        map.put("mainDriverLicense", entity.getMainDriverLicense());
        map.put("viceDriverLicense", entity.getViceDriverLicense());
        map.put("imageName", entity.getImageName());
        map.put("imageIdCard", entity.getImageIdCard());
        map.put("imageFileNumber", entity.getImageFileNumber());
        map.put("name", entity.getImageName());
        map.put("idCard", entity.getImageIdCard());
        map.put("fileNumber", entity.getImageFileNumber());
        map.put("imageDriverSLicenseAuthority", entity.getImageDriverSLicenseAuthority());
        map.put("imageValidityPeriodStart", entity.getImageValidityPeriodStart());
        map.put("imageValidityPeriodEnd", entity.getImageValidityPeriodEnd());
        map.put("imageQuasiDrivingModels", entity.getImageQuasiDrivingModels());
        map.put("manualCheck", entity.isManualCheck());
        if (ldDriverAdress!=null){
            map.put("ipProvinceNo",ldDriverAdress.getIpProvinceNo());
            map.put("ipProvinceName",ldDriverAdress.getIpProvinceName());
            map.put("ipCityNo",ldDriverAdress.getIpCityNo());
            map.put("ipCityName",ldDriverAdress.getIpCityName());
            map.put("ipDistrictNo",ldDriverAdress.getIpDistrictNo());
            map.put("ipDistrictName",ldDriverAdress.getIpDistrictName());
            map.put("ipLongitude",ldDriverAdress.getIpLongitude());
            map.put("ipLatitude",ldDriverAdress.getIpLatitude());
        }

        NewAPIManager.getInstance().getLogisticsAPI().newRegisterToProDriver(map).enqueue(new Callback<BaseEntity<Object>>() {
            @Override
            public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    registerLD.postValue((String) response.body().getData());
                } else if (response.body().getState().equals(APIManager.RESPONSE_FAIL) && "400".equals(response.body().getCode())) {
                    registerLD.postValue("400");
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    /**
     * 获取短信验证码
     */
    public void getCode(String phoneNum) {
        NewAPIManager.getInstance().getLogisticsAPI().getPhoneVerifyCode(phoneNum).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                codeLD.postValue(true);
            }
        });
    }

    /**
     * 上传图片
     */
    public void uploadMutFileModel(NewProprietaryDriverActivity activity, File file, int type) {
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + "image.jpg\"", requestBody);
        DialogHelper.showProgressDialog(activity, "文件上传中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUploadAPI().uploadFile(photos).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                activity.uploadImgSuccess(data.getData().toString(), type);
            }
        });
    }

    /**
     * 获取OCR的识别信息
     *
     * @param activity
     * @param mainDriverLicense
     * @param viceDriverLicense
     */
    public void getOCRLicenceInfo(NewProprietaryDriverActivity activity, String mainDriverLicense, String viceDriverLicense) {
        DialogHelper.showProgressDialog(activity, "文件上传中...").setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getLogisticsAPI().getOCRLicenceInfo(mainDriverLicense, viceDriverLicense).enqueue(new Callback<BaseEntity<LicenceOCREntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<LicenceOCREntity>> call, Response<BaseEntity<LicenceOCREntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_FAIL)) {
                    OCRLD.postValue(null);
                } else if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null) {
                        OCRLD.postValue(new LicenceOCREntity());
                    } else {
                        OCRLD.postValue(response.body().getData());
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<LicenceOCREntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity.getApplicationContext(), t.getMessage());
            }
        });
    }
    /**
     * 获取定位信息
     */
    public void getLocation(Context context) {
        try {
            AddressHelper.getInstance().getCurrentLocation(context, entity -> {
                if (entity == null ) return;
                if (ldDriverAdress==null) return;
                ldDriverAdress.setIpLatitude(entity.getLatitude());
                ldDriverAdress.setIpLongitude(entity.getLongitude());
                ldDriverAdress.setIpProvinceName(entity.getProvince());
                ldDriverAdress.setIpProvinceNo(entity.getProvinceNo());
                ldDriverAdress.setIpCityName(entity.getCity());
                ldDriverAdress.setIpCityNo(entity.getCityNo());
                ldDriverAdress.setIpDistrictName(entity.getDistrict());
                ldDriverAdress.setIpDistrictNo(entity.getDistrictNo());
            });
        }catch (Exception e){
        }
    }
}
